﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;

namespace NetDon.OA.Business
{
    public partial class RevenueMachineAccountService : IRevenueMachineAccountService
    {

        public IEnumerable<RevenueMachineAccountInvoiceReceiptData> GetRevenueMachineAccountInvoiceReceipts(Guid revenueMachineAccountInvoiceId)
        {
            var meta = new LinqMetaData();
            var data = (from revenueMachineAccount in meta.RevenueMachineAccountInvoiceReceipt
                        where revenueMachineAccount.RevenueMachineAccountInvoiceId == revenueMachineAccountInvoiceId
                        select revenueMachineAccount).ToList();

            return data.Select(d => GetRevenueMachineAccountReceiptData(d));

        }

        public IEnumerable<RevenueMachineAccountInvoiceReceiptData> GetRevenueMachineAccountInvoiceReceipts(Guid? revenueMachineAccountInvoiceId, RevenueMachineAccountInvoiceReceiptsSearchCriteria criteria)
        {
            var meta = new LinqMetaData();
            var data = (from revenueMachineAccount in meta.RevenueMachineAccountInvoiceReceipt
           
                        select revenueMachineAccount);
            if (criteria.AccountReceiptType.HasValue)
            {
                data = data.Where(d => d.AccountsReceiptType == criteria.AccountReceiptType);
            }
            if(revenueMachineAccountInvoiceId.HasValue)
            {
                 data = data.Where(d => d.RevenueMachineAccountInvoiceId == revenueMachineAccountInvoiceId);
       
            }

            return data.ToList().Select(d => GetRevenueMachineAccountReceiptData(d));

        }

        public RevenueMachineAccountInvoiceReceiptData GetRevenueMachineAccountInvoiceReceipt(Guid revenueMachineAccountInvoiceId)
        {
            return GetRevenueMachineAccountReceiptData(new RevenueMachineAccountInvoiceReceiptEntity(revenueMachineAccountInvoiceId));
        }

        public bool ModifyRevenueMachineAccountInvoiceReceipt(RevenueMachineAccountInvoiceReceiptData entity)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "ModifyRevenueMachineAccountInvoiceReceipt");
            try
            {
                var machineAccountEntity = new RevenueMachineAccountInvoiceReceiptEntity(entity.Id);
                GetRevenueMachineAccountReceiptEntity(machineAccountEntity, entity);
                trans.Add(machineAccountEntity);
                machineAccountEntity.Save();

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }

        }

        public bool AddRevenueMachineAccountInvoiceReceipt(RevenueMachineAccountInvoiceReceiptData data)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "AddRevenueMachineAccountInvoiceReceipt");
            try
            {
    

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public bool RemoveRevenueMachineAccountReceipt(Guid revenueMachineAccountInvoiceId)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveRevenueMachineAccountReceipt");
            try
            {

                var machineAccountEntity = new RevenueMachineAccountInvoiceReceiptEntity(revenueMachineAccountInvoiceId);
                trans.Add(machineAccountEntity);
                machineAccountEntity.Delete();

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }



        private void AddRevenueMachineAccountInvoiceReceipt(RevenueMachineAccountInvoiceData revenueMachineAccountInvoiceData, Transaction trans, RevenueMachineAccountInvoiceEntity revenueMachineAccountInvoiceEntity)
        {
            foreach (var revenueMachineAccountInvoiceReceipt in revenueMachineAccountInvoiceData.RevenueMachineAccountInvoiceReceipts)
            {

                var revenueMachineAccountInvoiceReceiptEntity = new RevenueMachineAccountInvoiceReceiptEntity(revenueMachineAccountInvoiceReceipt.Id);
                if (revenueMachineAccountInvoiceReceiptEntity.IsNew)
                {
                    if (string.IsNullOrWhiteSpace(revenueMachineAccountInvoiceReceipt.BankNoteNO))
                    {
                        continue;
                    }
                    revenueMachineAccountInvoiceReceiptEntity.Id = Guid.NewGuid();

                }
                else
                {
                    if (string.IsNullOrWhiteSpace(revenueMachineAccountInvoiceReceipt.BankNoteNO))
                    {
                        revenueMachineAccountInvoiceReceiptEntity.Delete();
                        trans.Add(revenueMachineAccountInvoiceReceiptEntity);
                        revenueMachineAccountInvoiceReceiptEntity.Save();
                     
                        continue;
                    }
                }
                GetRevenueMachineAccountReceiptEntity(revenueMachineAccountInvoiceReceiptEntity, revenueMachineAccountInvoiceReceipt);

                revenueMachineAccountInvoiceReceiptEntity.RevenueMachineAccountInvoiceId = revenueMachineAccountInvoiceEntity.Id;
                trans.Add(revenueMachineAccountInvoiceReceiptEntity);
                revenueMachineAccountInvoiceReceiptEntity.Save();

            }
        }


        #region private
        private RevenueMachineAccountInvoiceReceiptData GetRevenueMachineAccountReceiptData(RevenueMachineAccountInvoiceReceiptEntity entity)
        {
            var revenueMachineAccountData = new RevenueMachineAccountInvoiceReceiptData()
            {
                Id = entity.Id,
                AreaId =
                    entity.AreaId,
                RevenueMachineAccountInvoiceId = entity.RevenueMachineAccountInvoiceId,
                BankNoteNO = entity.BankNoteNo,
                CurrencyType = entity.CurrencyType,
                AccountsReceiptAmount = entity.AccountsReceiptAmount,
                AccountsReceiptType = entity.AccountsReceiptType,
                ConfirmAccountsReceiptDate = entity.ConfirmAccountsReceiptDate,
                BillOfExchangeDueDate = entity.BillOfExchangeDueDate,
                Comments = entity.Comments,
                CreateBy = entity.CreateBy,
                CreatedDate = entity.CreatedDate,
                LastUpdatedBy = entity.LastUpdatedBy,
                LastUpdatedDate = entity.LastUpdatedDate,

            };

            return revenueMachineAccountData;
        }
        private RevenueMachineAccountInvoiceReceiptEntity GetRevenueMachineAccountReceiptEntity(RevenueMachineAccountInvoiceReceiptEntity revenueMachineAccountEntity, RevenueMachineAccountInvoiceReceiptData revenueMachineAccountData)
        {

            revenueMachineAccountEntity.Id = revenueMachineAccountData.Id;
            revenueMachineAccountEntity.AreaId = revenueMachineAccountData.AreaId;

            revenueMachineAccountEntity.RevenueMachineAccountInvoiceId = revenueMachineAccountData.RevenueMachineAccountInvoiceId.Value;
            revenueMachineAccountEntity.BankNoteNo = revenueMachineAccountData.BankNoteNO;
            revenueMachineAccountEntity.CurrencyType = revenueMachineAccountData.CurrencyType;
            revenueMachineAccountEntity.AccountsReceiptAmount = revenueMachineAccountData.AccountsReceiptAmount;
            revenueMachineAccountEntity.AccountsReceiptType = revenueMachineAccountData.AccountsReceiptType;
            revenueMachineAccountEntity.ConfirmAccountsReceiptDate = revenueMachineAccountData.ConfirmAccountsReceiptDate;
            revenueMachineAccountEntity.BillOfExchangeDueDate = revenueMachineAccountData.BillOfExchangeDueDate;
            revenueMachineAccountEntity.Comments = revenueMachineAccountData.Comments;

            revenueMachineAccountEntity.CreateBy = revenueMachineAccountData.CreateBy.Value;
            revenueMachineAccountEntity.CreatedDate = revenueMachineAccountData.CreatedDate.Value;
            revenueMachineAccountEntity.LastUpdatedBy = revenueMachineAccountData.LastUpdatedBy.Value;
            revenueMachineAccountEntity.LastUpdatedDate = revenueMachineAccountData.LastUpdatedDate.Value;

            return revenueMachineAccountEntity;
        }

        #endregion
    }
}
